//
//  NetworkTools.m
//  AFN-OC封装
//
//  Created by user on 16/4/18.
//  Copyright © 2016年 user. All rights reserved.
//

#import "NetworkTools.h"

@protocol NetworkToolsDelegate  <NSObject>

@optional

- (NSURLSessionDataTask *)dataTaskWithHTTPMethod:(NSString *)method
                                       URLString:(NSString *)URLString
                                      parameters:(id)parameters
                                      uploadProgress:(nullable void (^)(NSProgress *uploadProgress)) uploadProgress
                                      downloadProgress:(nullable void (^)(NSProgress *downloadProgress)) downloadProgress
                                      success:(void (^)(NSURLSessionDataTask *, id))success
                                      failure:(void (^)(NSURLSessionDataTask *, NSError *))failure;

@end

@interface NetworkTools ()<NetworkToolsDelegate>

@end

@implementation NetworkTools

+(instancetype)sharedTools{
    
    static NetworkTools *instance;
    
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        
        instance = [[NetworkTools alloc] init];
        
        //text/plain
        instance.responseSerializer.acceptableContentTypes = [instance.responseSerializer.acceptableContentTypes setByAddingObject:@"text/html"];
        instance.responseSerializer.acceptableContentTypes = [instance.responseSerializer.acceptableContentTypes setByAddingObject:@"text/plain"];
    });
    
    return instance;
}

- (void)upload:(NSString *)urlString and:(NSDictionary *)datas andParameter:(id)parameter andBlock:(void(^)(id , NSError *))finished{
    
    [[self POST:urlString parameters:parameter constructingBodyWithBlock:^(id<AFMultipartFormData>  _Nonnull formData) {
        
        [datas enumerateKeysAndObjectsWithOptions:0 usingBlock:^(id  _Nonnull key, id  _Nonnull obj, BOOL * _Nonnull stop) {
            
             [formData appendPartWithFileData:obj name:key fileName:@"haha.png" mimeType:@"application/octet-stream"];
        }];
        
    } progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
        
    }] resume];
    
    
}

- (void)request:(FANRequestMethod)method andUrlString:(NSString *)urlString andParameter:(id)parameter andBlock:(void(^)(id , NSError *))finished{
    
    NSString *methodString = (method == FANRequestMethodGet) ? @"GET":@"POST";
    
    [[self dataTaskWithHTTPMethod:methodString URLString:urlString parameters:parameter uploadProgress:nil downloadProgress:nil success:^(NSURLSessionDataTask *dataTask, id response) {
        
        if (finished) {
            
            finished(response,nil);
        }
        
    } failure:^(NSURLSessionDataTask *dataTask, NSError *error) {
        
        if (finished) {
            
            finished(nil,error);
        }
    }] resume];
    
}

//- (void)demo{
//    
//    [self GET:@"post" parameters:nil progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
//        
//        
//    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
//        
//        
//    }];
//}

@end
